package com.lida.study;

import org.opencv.core.Core;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.lang.reflect.Field;

/**
 * @author lD
 * @date 2019/7/10 13:39
 */
@SpringBootApplication
@EnableScheduling
public class Application extends SpringBootServletInitializer {

    // 加载opencv dll文件
    static {
        addDirToPath("F:\\opencv\\opencv\\build\\java\\x64\\");
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    private static void addDirToPath(String s) {
        try {
            //获取系统path变量对象
            Field field = ClassLoader.class.getDeclaredField("sys_paths");
            //设置此变量对象可访问
            field.setAccessible(true);
            //获取此变量对象的值
            String[] path = (String[]) field.get(null);
            //创建字符串数组，在原来的数组长度上增加一个，用于存放增加的目录
            String[] tem = new String[path.length + 1];
            //将原来的path变量复制到tem中
            System.arraycopy(path, 0, tem, 0, path.length);
            //将增加的目录存入新的变量数组中
            tem[path.length] = s;
            //将增加目录后的数组赋给path变量对象
            field.set(null, tem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Application.class);
    }
}
